package com.burgeon.backend.Rest.User;
/**
 * 获取零售单明细
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.RetailRecordItem;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetRetailItem extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetRetailItem(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<RetailRecordItem> getData(String id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(id), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)) {
            throw new Exception(message);
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<RetailRecordItem> data = new ArrayList();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            RetailRecordItem retailRecordItem = new RetailRecordItem();
            retailRecordItem.setId(array.getString(0));
            retailRecordItem.setCode(array.getString(1));
            retailRecordItem.setName(array.getString(2));
            retailRecordItem.setOriginal(array.getString(3));
            retailRecordItem.setSelling(array.getString(4));
            retailRecordItem.setQty(array.getString(5));
            data.add(retailRecordItem);


        }

        return data;
    }

    private MultiValueMap Data(String id) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");
        columns.add("'M_PRODUCTALIAS_ID'");//条码
        columns.add("'M_PRODUCT_ID;VALUE'");//商品名称
        columns.add("PRICELIST");//售价
        columns.add("PRICEACTUAL");//成交价
        columns.add("QTY");//数量


        params.put("columns", columns);
        params.put("table", "M_RETAILITEM");//表名

        params.put("count", "true");

        HashMap orderby = new HashMap();

        orderby.put("column", "OPENCARDDATE");
        orderby.put("asc", "false");

        HashMap paramss = new HashMap();
        String ac = "'=" + id + "'";
        paramss.put("column", "M_RETAIL_ID");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetRetailItem getRetailByVip = new GetRetailItem("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        try {
            System.err.println(getRetailByVip.getData("457751"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
